package css.sword.fastdfs;

import java.io.*;
import java.net.*;

public class TrackerServer
{
	protected Socket sock;
	protected InetSocketAddress inetSockAddr;

	public TrackerServer(Socket sock, InetSocketAddress inetSockAddr)
	{
		this.sock = sock;
		this.inetSockAddr = inetSockAddr;
	}
	
	public Socket getSocket() throws IOException
	{
		if (this.sock == null)
		{
			this.sock = TrackerClient.getSocket(this.inetSockAddr);
		}
		
		return this.sock;
	}
	
	public InetSocketAddress getInetSocketAddress()
	{
		return this.inetSockAddr;
	}
	
	public OutputStream getOutputStream() throws IOException
	{
		return this.sock.getOutputStream();
	}
	
	public InputStream getInputStream() throws IOException
	{
		return this.sock.getInputStream();
	}

	public void close() throws IOException
	{
		if (this.sock != null)
		{
			try
			{
				ProtoCommon.closeSocket(this.sock);
			}
			finally
			{
				this.sock = null;
			}
		}
	}
	
	protected void finalize() throws Throwable
	{
		this.close();
	}
}
